package sealte.hvpms.servlet.vp;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.hvpms.dao.PollOptionDAO;
import sealte.hvpms.dao.RuleDAO;
import sealte.hvpms.dao.impl.PollDAOImpl;
import sealte.hvpms.dao.impl.PollOptionDAOImpl;
import sealte.hvpms.dao.impl.RuleDAOImpl;
import sealte.hvpms.util.HVPMSTools;
import sealte.hvpms.util.IdcardUtils;

public class VPVote extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("Cache-Control", "no-cache");
		request.setCharacterEncoding("UTF-8"); 
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String fid_str = request.getParameter("fid");
		if(fid_str!=null&&!fid_str.equals("")&&HVPMSTools.isNum(fid_str)){
			int fid = Integer.parseInt(fid_str);
			int rule = (new PollDAOImpl()).getPollRuleByID(fid);
			String message = "";
			boolean isOK = false;
			if(rule==1){
				isOK = true;
			}else if(rule==2){
				String rand = (String)request.getSession().getAttribute("rand");
				String validateCode = request.getParameter("validateCode");
				if(validateCode.equals(rand)){
					isOK = true;
				}else{
					message = "验证码不正确";
				}
			}else if(rule==3){
				String voter_email = request.getParameter("voter_email");
				RuleDAO rb = new RuleDAOImpl();
				boolean isEmailExist = rb.isEmailExist(fid, voter_email);
				if(isEmailExist){
					message = "您已经用该邮箱注册过一次投票，禁止重复投票";
				}else{
					rb.addEmail(fid, voter_email);
					isOK = true;
				}
			}else if(rule==4){
				String voter_ipaddr = request.getParameter("voter_ipaddr");
				RuleDAO rb = new RuleDAOImpl();
				boolean isIPAddrExist = rb.isIPAddrExist(fid, voter_ipaddr);
				if(isIPAddrExist){
					message = "禁止同一个IP重复投票";
				}else{
					rb.addIPAddr(fid, voter_ipaddr);
					isOK = true;
				}
			}else if(rule==5){
				String voter_idcard = request.getParameter("voter_idcard");
				if(IdcardUtils.validateCard(voter_idcard)){
					RuleDAO rb = new RuleDAOImpl();
					boolean isIDCardExist = rb.isIDCardExist(fid, voter_idcard);
					if(isIDCardExist){
						message = "您已经用该身份证号注册过一次投票，禁止重复投票";
					}else{
						rb.addIDCard(fid, voter_idcard);
						isOK = true;
					}
				}else{
					message = "您的身份证号格式不正确";
				}
			}
			if(isOK){
				String[] pos_str = request.getParameterValues("poll_options");
				int[] pos = new int[pos_str.length];
				for(int i=0;i<pos_str.length;i++){
					pos[i] = Integer.parseInt(pos_str[i]);
				}
				PollOptionDAO pob = new PollOptionDAOImpl();
				boolean flag = pob.doVote(fid, pos);
				if(flag){
					request.getRequestDispatcher("vp_res_view.jsp").forward(request, response);
				}else{
					message = "投票失败，请重新投票";
					request.setAttribute("message", message);
					request.getRequestDispatcher("vp_view.jsp").forward(request, response);
				}
			}else{
				request.setAttribute("message", message);
				request.getRequestDispatcher("vp_view.jsp").forward(request, response);
			}
		}else{
			out.println("系统出错。。。");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
